using System;
using MySql.Data.MySqlClient;

namespace Scenario.Modules.DM.DataModel
{
	public class XMLDocumentVersion : bOSdotNET.XML.Data.XMLObject, bOS.DataBase.bOSTable 
	{
		public const string MAIN_NODE		= "Version";

		private const string ID_ATTRIBUTE			= "Id";
		private const string DESCRIPTION_NODE		= "Description";
		private const string CREATIONDATE_ATTRIBUTE	= "CreationDate";
	
		private string sId;
		private string sDescription = string.Empty;
		private string sCreationDate = string.Empty;

		private XMLDocuments xmlDocuments;
		private XMLDocumentAttachFiles xmlDocAttachFiles;
        private Scenario.WorkflowEngine.DataModel.WorkflowProcesses.XMLReports xmlReports;
        private Scenario.WorkflowEngine.DataModel.WorkflowProcesses.XMLLogs xmlLogs;
        private Scenario.WorkflowEngine.DataModel.WorkflowRelevantData.XMLAttributes wfAttributes;

		public XMLDocumentVersion()
		{
			this.Init();
		}

		public XMLDocumentVersion(System.Xml.XmlNode node)
		{
			this.Parse(node);
		}

		public string Id 
		{
			get { return sId; }
			set { sId = value; }
		}

		public string Description 
		{
			get { return sDescription; }
			set { sDescription = value; }
		}

		public string CreationDate 
		{
			get { return sCreationDate; }
			set { sCreationDate = value; }
		}

        public Scenario.WorkflowEngine.DataModel.WorkflowProcesses.XMLReports XMLReports
		{
			get { return xmlReports;}
			set { xmlReports = value;}
		}

        public Scenario.WorkflowEngine.DataModel.WorkflowProcesses.XMLLogs XMLLogs
		{
			get { return xmlLogs;}
			set { xmlLogs = value;}
		}

		public XMLDocumentAttachFiles XMLDocumentAttachFiles
		{
			get { return xmlDocAttachFiles; }
			set { xmlDocAttachFiles = value; }
		}

		public XMLDocuments XMLDocuments
		{
			get { return xmlDocuments; }
			set { xmlDocuments = value; }
		}

        public Scenario.WorkflowEngine.DataModel.WorkflowRelevantData.XMLAttributes XMLAttributes
		{
			get {return wfAttributes;}
			set {wfAttributes=value;}
		}

		override public void Init()
		{
			xmlDocuments = new XMLDocuments(false);
			xmlDocAttachFiles = new XMLDocumentAttachFiles();
			xmlReports = new Scenario.WorkflowEngine.DataModel.WorkflowProcesses.XMLReports();
			xmlLogs = new Scenario.WorkflowEngine.DataModel.WorkflowProcesses.XMLLogs();
			wfAttributes = new Scenario.WorkflowEngine.DataModel.WorkflowRelevantData.XMLAttributes();

		}

		override public void Parse(System.Xml.XmlNode node2Parse)
		{
			this.Init();

			System.Xml.XmlNode node;

			for ( int i=0; i<node2Parse.Attributes.Count;i++)
			{
				node = node2Parse.Attributes.Item(i);

				switch ( node.Name )
				{
					case XMLDocumentVersion.ID_ATTRIBUTE:
						sId = node.InnerText;
						break;

					case XMLDocumentVersion.CREATIONDATE_ATTRIBUTE:
						this.CreationDate = node.InnerText;
						break;
				}
			}

			for ( int i=0; i<node2Parse.ChildNodes.Count; i++)
			{
				node = node2Parse.ChildNodes[i];

				switch ( node.Name )
				{
					case XMLDocuments.MAIN_NODE:
						xmlDocuments.Parse (node);
						break;

					case XMLDocumentAttachFiles.MAIN_NODE:
						xmlDocAttachFiles.Parse (node);
						break;

                    case Scenario.WorkflowEngine.DataModel.WorkflowProcesses.XMLReports.MAIN_NODE:
						xmlReports.Parse (node);
						break;

                    case Scenario.WorkflowEngine.DataModel.WorkflowProcesses.XMLLogs.MAIN_NODE:
						xmlLogs.Parse(node);
						break;

					case XMLDocumentVersion.DESCRIPTION_NODE:
						this.Description = node.InnerText;
						break;

					case Scenario.WorkflowEngine.DataModel.WorkflowRelevantData.XMLAttributes.MAIN_NODE:
						this.wfAttributes.Parse(node);
						break;
				}
			}
		}

		override public bOSdotNET.XML.XMLNode GetXMLNode()
		{
			bOSdotNET.XML.XMLNode xmlNode = new bOSdotNET.XML.XMLNode(XMLDocumentVersion.MAIN_NODE);
			xmlNode.AddAttribute ( new bOSdotNET.XML.XMLAttribute( XMLDocumentVersion.ID_ATTRIBUTE, sId) );
			xmlNode.AddAttribute ( new bOSdotNET.XML.XMLAttribute( XMLDocumentVersion.CREATIONDATE_ATTRIBUTE, this.CreationDate) );

			xmlNode.AddNode ( xmlDocuments.GetXMLNode());
			xmlNode.AddNode ( xmlDocAttachFiles.GetXMLNode() );

			xmlNode.AddNode ( xmlReports.GetXMLNode());
			xmlNode.AddNode ( xmlLogs.GetXMLNode() );
			xmlNode.AddNode( wfAttributes.GetXMLNode());

			xmlNode.AddNode ( new bOSdotNET.XML.XMLNode (XMLDocumentVersion.DESCRIPTION_NODE, this.Description) );

			return xmlNode;
		}

		public void ReadFromDataBase(MySqlDataReader reader)
		{
			while (reader.Read() ) 
			{
				//modified by e.leano for dm step 2
				this.Id = reader.GetValue(5).ToString();
				this.Description = reader.GetValue(1).ToString();
				this.CreationDate = reader.GetValue(2).ToString();
				this.xmlDocAttachFiles.Parse ( reader.GetValue(3).ToString());
				this.xmlReports.Parse ( reader.GetValue(7).ToString());
				this.xmlLogs.Parse ( reader.GetValue(6).ToString());
				
				
				
			}
		}
	}
}
